package com.example.goodtrash.search;

import android.app.Activity;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodtrash.R;
import com.example.goodtrash.model.Trash;

import java.util.List;
import java.util.Locale;

public class TrashAdapter extends RecyclerView.Adapter<TrashAdapter.ViewHolder> {
    private List<Trash> mtrashList;
    private Context mcontext;
    private TextToSpeech textToSpeech=null;
    private Activity mActivity;
    static class ViewHolder extends RecyclerView.ViewHolder{
          View mview;
          TextView tvName;
          TextView tvType;
          TextView tvExplain;

          public ViewHolder(View view){
              super(view);
              mview=view;
              tvName=(TextView) view.findViewById(R.id.tv_name);
              tvType=(TextView) view.findViewById(R.id.tv_type);
              tvExplain=(TextView) view.findViewById(R.id.tv_explain);
          }
    }

    public TrashAdapter(List<Trash> trashList,Context context,Activity activity){
        mtrashList=trashList;
        mcontext=context;
        mActivity=activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_rv,parent,false);
        ViewHolder holder=new ViewHolder(view);
        initTTS();
        holder.mview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                Trash trash=mtrashList.get(position);
                startAuto(trash.getTrashName()+trash.getTrashType()+trash.getTrashTip());

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Trash trash=mtrashList.get(position);
        holder.tvName.setText(trash.getTrashName());
        holder.tvType.setText(trash.getTrashType());
        holder.tvExplain.setText(trash.getTrashTip());

    }

    @Override
    public int getItemCount() {
        return mtrashList.size();
    }

    public void initTTS(){
        textToSpeech=new TextToSpeech(mActivity, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i==TextToSpeech.SUCCESS){
                    textToSpeech.setPitch(1.0f);
                    textToSpeech.setSpeechRate(1.0f);
                    textToSpeech.setLanguage(Locale.CHINESE);

                }else {
                    Toast.makeText(mcontext,"数据丢失",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    private void startAuto(String data){
        textToSpeech.setPitch(1.0f);
        textToSpeech.setSpeechRate(0.3f);
        textToSpeech.speak(data,TextToSpeech.QUEUE_FLUSH,null);
    }


}
